use std::collections::BTreeMap;

use jwt::token::Signed;
use jwt::{Error, Header, SignWithKey, Token};

fn create_jwt_token<T: jwt::SigningAlgorithm>(key: T) -> Result<String, Error> {
    let mut claims = BTreeMap::new();
    claims.insert("sub", "someone");
    claims.sign_with_key(&key)
}

fn create_jwt_token_header<T: jwt::SigningAlgorithm>(
    key: T,
) -> Result<Token<Header, BTreeMap<&'static str, &'static str>, Signed>, Error> {
    let header = jwt::Header {
        algorithm: jwt::AlgorithmType::Hs384,
        ..Default::default()
    };
    let mut claims = BTreeMap::new();
    claims.insert("sub", "someone");
    jwt::Token::new(header, claims).sign_with_key(&key)
}

#[cfg(test)]
mod filex {
    use std::collections::BTreeMap;

    use hmac::{Hmac, Mac};
    use jwt::{AlgorithmType, SignWithKey, VerifyWithKey};
    use sha2::{Sha256, Sha384};

    use crate::utilx::auth::jwt::create_jwt_token;

    #[test]
    fn base() {
        let wrapper: Hmac<Sha256> = Hmac::new_from_slice(b"asd").unwrap();
        let string = create_jwt_token(wrapper).unwrap();
        println!("{}", &string);
        let aa: Hmac<Sha256> = Hmac::new_from_slice(b"asd").unwrap();
        let claims: BTreeMap<String, String> = string.verify_with_key(&aa).unwrap();

        println!("{:?}", claims);
    }

    #[test]
    fn asasd() {
        let key: Hmac<Sha384> = Hmac::new_from_slice(b"some-secret").unwrap();
        let header = jwt::Header {
            algorithm: AlgorithmType::Hs384,
            ..Default::default()
        };
        let mut claims = BTreeMap::new();
        claims.insert("sub", "someone");
        let token = jwt::Token::new(header, claims).sign_with_key(&key).unwrap();
        println!("{}", token.as_str());

        println!("==============================");

        let token: jwt::Token<jwt::Header, BTreeMap<String, String>, _> =
            token.as_str().verify_with_key(&key).unwrap();
        let header = token.header();
        let claims = token.claims();
        println!("{:?}", header.algorithm);
        println!("{:?}", claims);
    }
}
